package io.bdmc.modules.bss.controller;

import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import io.bdmc.common.utils.ExcelImport;
import io.bdmc.common.utils.MyMap;
import io.bdmc.common.utils.MyPage;
import io.bdmc.common.utils.R;
import io.bdmc.modules.bss.entity.BarDev;
import io.bdmc.modules.bss.entity.BarRealtime;
import io.bdmc.modules.bss.model.BarDevParam;
import io.bdmc.modules.bss.service.BarDevService;
import io.bdmc.modules.bss.service.BarRealtimeService;
import io.bdmc.modules.sys.controller.AbstractController;

@RestController
@RequestMapping("/bss/sim")
public class SimController extends AbstractController {

    @Autowired
    private BarDevService devSvs;

    @Autowired
    private BarRealtimeService realSvs;

    @RequestMapping("/list")
    // @RequiresPermissions("sys:dev:list")
    public R list(BarDevParam params) {
        // 查询列表数据
        MyPage<BarDev> myPage = new MyPage<BarDev>(params.getCurrent(), params.getSize());
        params.setDevStatus(2);
        MyPage<BarDev> map = devSvs.mySelectPageSim(myPage, params);
        return R.ok().putData(MyMap.ok(map.getRecords(), map.getTotal()));
    }

    @RequestMapping("import")
    @RequiresPermissions("bss:sim:import")
    // @ApiOperation(value = "导入Excel",notes = "将数据以Excel的形式导入到数据库中")
    public R importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        // 获取用户上传表格的地址，执行完程序后表格将会删除，避免占用内存
        // 根据地址和文件名准确获取用户上传的表格
        // File tempFile = new File(file.getInputStream());
        try {
            // 将MultipartFile转换为File类型
            // file.transferTo(tempFile);
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            InputStream in = file.getInputStream();
            List<Map<String, String>> dataList = ExcelImport.read(in, suffix);
            BarDev dev = new BarDev();
            
            List<BarDev> list = new ArrayList<BarDev>();
            List<BarRealtime> realList = new ArrayList<BarRealtime>();
            for (int i = 0; i < dataList.size(); i++) {
                if (StringUtils.isEmpty(dataList.get(i).get("sim"))) {
                    continue;
                }
                String simNum = dataList.get(i).get("sim").toString();
                dev = (BarDev) dev.clone();
                dev.setSim(simNum);
                dev.setDeptId(getDeptId());
                dev.setCreateTime(LocalDateTime.now());
                dev.setCreateUser(getUserId());
                dev.setDevStatus(1);
                BarRealtime real = new BarRealtime();
                real.setCreateTime(LocalDateTime.now());
                real.setSim(simNum);
                real.setIsAlarm(false);
                list.add(dev);
                realList.add(real);
            }

            Boolean re = devSvs.saveBatch(list);
            Boolean re2 = realSvs.saveBatch(realList);
            //加入实时表
           if(re&&re2){
            return R.ok(re.toString());
           }else{
            return R.error();
           }
        } catch (Exception e) {
            // tempFile.delete();
            throw new Exception("批量录入用户失败，请检查表格中的数据是否和数据库中的数据冲突！");
        }
    }



    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("bss:sim:delete")
    public R delete(@RequestBody long devId) {
        if (devId > 0)
            devSvs.removeById(devId);

        return R.ok();
    }
}